/****************************************************
          File Name:mq_open.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sat 20 Sep 2014 08:40:01 AM CST
*****************************************************/

#include"posix_ipc.h"
#include<getopt.h>
#include<string.h>
#include<unistd.h>
#define FLAG O_RDWR

void usage(char *arg)
{
    printf("%s -c /mq\nor\n%s -n -c /mq (O_NONBLOCK)\n",arg,arg);
}

int main(int argc,char *argv[])
{
    mqd_t mq;
    int opt, flag;

    if(argc<2||strcmp(argv[1],"--help")==0)
    {
        usage(argv[0]);
        exit(EXIT_FAILURE);
    }

    while((opt=getopt(argc,argv,"c:n"))!=-1)
    {
        switch(opt)
        {
            case 'c':
                flag=FLAG|O_CREAT;
                if(optarg==NULL)
                    usage(argv[0]);
                break;
            case 'n':
                flag=FLAG|O_CREAT|O_NONBLOCK;
                break;
        }
    }
    mq=mq_open(argv[optind-1],flag,MODE,NULL);
    if(mq==(mqd_t)-1)
        err("mq_open");
    printf("%i \n",mq);
    exit(EXIT_SUCCESS);
}
